package com.motorola.dp.jenkinsmonitor.service.util.svn;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.motorola.dp.jenkinsmonitor.model.util.HttpRequestParameters;
import com.motorola.dp.jenkinsmonitor.service.util.properties.PropertiesService;

@Component("svnServiceHelper")
public class SvnServiceHelper {

    @Value("#{applicationPropertiesService}")
    private PropertiesService propertiesService;

    public HttpRequestParameters createSvnResourceRequest(String resourcePath) {
        String resourceUrl = createServerUrl() + resourcePath;
        return createSvnRequestParams(resourceUrl);
    }

    public HttpRequestParameters createSvnResourceRequest(String moduleName, String resourcePath) {
        String resourceUrl = createModuleUrl(moduleName) + resourcePath;
        return createSvnRequestParams(resourceUrl);
    }

    private HttpRequestParameters createSvnRequestParams(String resourceUrl) {
        String username = propertiesService.getString("svn.username");
        String password = propertiesService.getString("svn.password");

        HttpRequestParameters requestParams = new HttpRequestParameters(resourceUrl, username, password);
        return requestParams;
    }

    private String createModuleUrl(String moduleName) {
        StringBuilder sb = new StringBuilder();
        sb.append(createServerUrl());
        sb.append(moduleName);
        sb.append("/trunk/");

        return sb.toString();
    }

    private String createServerUrl() {
        String server = propertiesService.getString("svn.server");

        StringBuilder sb = new StringBuilder();
        sb.append("https://");
        sb.append(server);
        sb.append("/");

        return sb.toString();
    }

    public void setPropertiesService(PropertiesService propertiesService) {
        this.propertiesService = propertiesService;
    }
}
